package touro.mancala.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class MancalaServer implements Runnable{
	
		private ServerSocket server;
		private RandomStrategy stretegy;
		private boolean closed;
		
		public MancalaServer()throws IOException{
			server = new ServerSocket(8080);
	
		}
		public void close(){
			try {
				closed=true;
				server.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public void run(){
			//handling the event loop
			while(!closed){
				try {
					handleSocket(server.accept());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		private void handleSocket(Socket s){
		
			try{

			OutputStream out=s.getOutputStream();
			stretegy=new RandomStrategy();
			
			String response=stretegy.getRandomBoard().toString();
			out.write(response.getBytes());
			s.close();
			}catch(IOException e){
				
			}
		}
		public static void main(String []args) throws IOException{
			new MancalaServer().run();
		}
}
